import PlantHeader from "../components/index/plant-header";
import SecondPlantHeader from "../components/second/plant-header";
import ajax from "./request";

const indexPlant = {
    components: { PlantHeader },
    props: {
        modelValue: {
            type: Array
        }
    },
    watch: {
        modelValue: {
            handler(newVal) {
                if (newVal.length > 0) {
                    this.selectedItem = newVal[0];
                }
            }
        },
        selectedItem: {
            handler(newVal) {
                // 需要查询普通列表
                if (this.listRows > 0) {
                    ajax("/content/queryList", { t_cat_plant_fk: newVal.t_cat_plant_pk, rows: this.listRows }).then(({ data }) => {
                        if (data.status > 0) {
                            this.listData = data.result_data;
                        }
                    });
                }
                // 需要查询有图列表
                if (this.swiperRows > 0) {
                    ajax("/content/queryBannerContent", { t_cat_plant_fk: newVal.t_cat_plant_pk, rows: this.swiperRows }).then(({ data }) => {
                        if (data.status > 0) {
                            this.swiperData = data.result_data;
                        }
                    });
                }
            }
        }
    },
    data() {
        return {
            listRows: 10,
            swiperRows: 4,
            selectedItem: {},
            swiperData: [],
            listData: [],
        }
    }
}

const secondPlant = {
    props: {
        modelValue: {
            type: Object,
            required: true
        }
    },
    watch: {
        modelValue: {
            immediate: true,
            handler(newVal) {
                if (!newVal) {
                    return;
                }
                // console.log("加载数据！newVal = ",newVal);
                // 需要查询普通列表
                if (this.listRows > 0) {
                    ajax("/content/queryList", { t_cat_plant_fk: newVal.t_cat_plant_pk, rows: this.listRows }).then(({ data }) => {
                        if (data.status > 0) {
                            this.listData = data.result_data;
                        }
                    });
                }
                // 需要查询有图列表
                if (this.swiperRows > 0) {
                    // 招生信息页面的招生动态，需要通过加载resource/queryList接口完成数据加载
                    if (newVal.t_cat_plant_pk == 388) {
                        ajax("/resource/queryList", { t_cat_plant_fk: 388, rows: 2, t_cat_fk: 29 }).then(({ data }) => {
                            if (data.status > 0) {
                                this.swiperData = data.result_data;
                            }
                        });
                    } else {
                        ajax("/content/queryBannerContent", { t_cat_plant_fk: newVal.t_cat_plant_pk, rows: this.swiperRows }).then(({ data }) => {
                            if (data.status > 0) {
                                this.swiperData = data.result_data;
                            }
                        });
                    }
                }
            }
        },
    },
    data() {
        return {
            listRows: 5,
            swiperRows: 4,
            swiperData: [],
            listData: [],
        }
    },
    components: {
        PlantHeader: SecondPlantHeader
    }
}

const urlMethods = {
    methods: {
        getListUrl(
            { cat_name = "首页", t_cat_pk = "5" },
            { plant_name, t_plant_pk, t_cat_plant_pk, url = "content-list" }) {
            // console.log("url = ", url);
            if (url == '/second/list/videopage') {
                url = 'video-list'
            } else if (url == '/second/list/teacher') {
                url = 'image-list'
            } else {
                url = "content-list"
            }
            return {
                name: url,
                params: {
                    cat_name,
                    t_cat_pk,
                    plant_name,
                    t_plant_pk,
                    t_cat_plant_pk,
                    url
                }
            }
        },
        getRowUrl({ cat_name = "首页", t_cat_pk = "5" },
            { plant_name, t_plant_pk, t_cat_plant_fk, t_content_pk }) {
                // console.log("plant_name:",plant_name,",t_cat_plant_pk:",t_cat_plant_fk,",t_content_pk",t_content_pk);
            return {
                name: 'content-row',
                params: {
                    cat_name,
                    t_cat_pk,
                    plant_name,
                    t_plant_pk,
                    t_cat_plant_pk: t_cat_plant_fk,
                    t_content_pk
                }
            }
        }
    }
}

export { indexPlant, secondPlant, urlMethods }